<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="initial-scale=1, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0" />
    <title>下拉刷新+无限加载</title>
    <style>
        html,
        body {
            margin: 0;
            padding: 0;
            height: 100%;
            background: #dadada;
        }

        header {
            height: 44px;
            line-height: 44px;
            text-align: center;
            background: #da020c;
            color: #fff;
        }

        header>a {
            height: 34px;
            line-height: 34px;
            position: absolute;
            top: 5px;
            left: 10px;
        }

        h1 {
            margin: 0;
            padding: 0;
            font-size: 18px;
            font-weight: normal;
        }

        #wrapper {
            position: absolute;
            top: 44px;
            bottom: 0;
            width: 100%;
        }

        #scroller {
            margin: 0;
            padding: 0;
        }

        .item {
            /*height: 60px;*/
            /*line-height: 60px;*/
            border-bottom: 1px solid #ddd;
            text-indent: 1em;
            background: #fff;
            list-style-type: none;
            padding: 10px 0;
        }
        .item_img{
            width: 100px;
            height: 100px;
        }
        .item_img img{
            width: 100%;
            height: 100%;
        }

        .jroll-infinite-tip {
            height: 44px;
            line-height: 44px;
            text-align: center;
        }
    </style>
    <script src="./js/jroll.js"></script>
    <script src="./js/jroll-pulldown.js"></script>
    <script src="./js/jroll-infinite.js"></script>
    <script src="./js/jquery.min.js"></script>
    <script src="./js/template.js"></script>
</head>

<body>
<header>
    <h1>下拉刷新+无限加载</h1>
    <a onclick="javascript:history.back()">&lt;返回</a>
</header>
<div id="wrapper">
    <div id="scroller"></div>
</div>

<script>
    //
    var jroll = new JRoll("#wrapper", {
        scrollBarY: true
    });

    //下拉刷新
    jroll.pulldown({
        refresh: function(complete) {
            jroll.options.page = 1;
            jroll.scrollTo(0, 44, 0, true);  //滚回顶部
            setTimeout(function () {
                $.ajax({
                    url:'/jroll',
                    type:'get',
                    success:function (data) {
                        complete();
                        jroll.scroller.innerHTML = "";    //清空内容
                        jroll.infinite_callback(data.data);//传给模板渲染
                    }
                })
            },800)
        },
        textLoading:"正在努力加载"
    });

    //上拉加载
    jroll.infinite({
        getData: function(page, callback) { //获取数据的函数，必须传递一个数组给callback
            setTimeout(function () {
                $.ajax({
                    url:'/jroll?page='+ page,
                    type:'get',
                    success:function (data) {
                        console.log(data.data);
                        console.log(data.total);
                        jroll.options.total = data.total;//动态获取总页数
                        callback(data.data);//
                    }
                })
            },800);
        },
        compile: function(text) {
            return template.compile(text);
        },
        template: "<div class='item'><a href='/comment?id = {{id}}'>{{id}}、{{content}}" +
        "{{if img_url }}<div class='item_img'><img src='{{img_url}}' alt=''></div>{{/if}}</a>" +
        "</div>" //每条数据模板
    });
</script>
</body>

</html>
